package com.wzz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzz.entity.Class;
import com.wzz.entity.ClassStudent;
import com.wzz.entity.User;
import com.wzz.service.ClassService;
import com.wzz.service.impl.UserServiceImpl;
import com.wzz.utils.JwtUtils;
import com.wzz.vo.CommonResult;
import com.wzz.vo.PageResponse;
import com.wzz.vo.TokenVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = "班级管理相关接口")
@RequestMapping("/class")
public class ClassController {

    private final ClassService classService;
    private final UserServiceImpl userService;

    @PostMapping("/create")
    @ApiOperation("创建班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "class", value = "班级信息", required = true, dataType = "Class", paramType = "body")
    })
    public CommonResult<Void> createClass(@RequestBody @Valid Class clazz) {
        classService.createClass(clazz);
        return CommonResult.<Void>builder().build();
    }

    @PostMapping("/update")
    @ApiOperation("更新班级信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "class", value = "班级信息", required = true, dataType = "Class", paramType = "body")
    })
    public CommonResult<Void> updateClass(@RequestBody @Valid Class clazz) {
        classService.updateClass(clazz);
        return CommonResult.<Void>builder().build();
    }

    @GetMapping("/delete/{classId}")
    @ApiOperation("删除班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "班级ID", required = true, dataType = "int", paramType = "path")
    })
    public CommonResult<Void> deleteClass(@PathVariable Integer classId) {
        classService.deleteClass(classId);
        return CommonResult.<Void>builder().build();
    }

    @GetMapping("/list")
    @ApiOperation("获取班级列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "teacherId", value = "教师ID", required = false, dataType = "int", paramType = "query")
    })
    public CommonResult<PageResponse<Class>> getClassList(
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) Integer teacherId) {
        return CommonResult.<PageResponse<Class>>builder()
                .data(classService.getClassList(pageNo, pageSize, teacherId))
                .build();
    }

    @PostMapping("/addStudent")
    @ApiOperation("添加学生到班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "班级ID", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "studentId", value = "学生ID", required = true, dataType = "int", paramType = "query")
    })
    public CommonResult<Void> addStudentToClass(@RequestParam Integer classId, @RequestParam Integer studentId) {
        classService.addStudentToClass(classId, studentId);
        return CommonResult.<Void>builder().build();
    }

    @PostMapping("/removeStudent")
    @ApiOperation("从班级移除学生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "班级ID", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "studentId", value = "学生ID", required = true, dataType = "int", paramType = "query")
    })
    public CommonResult<Void> removeStudentFromClass(
            @RequestParam Integer classId,
            @RequestParam Integer studentId) {
        classService.removeStudentFromClass(classId, studentId);
        return CommonResult.<Void>builder().build();
    }

    //查询班级学生
    @GetMapping("/getStudent")
    @ApiOperation("获取班级学生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "班级ID", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "int", paramType = "query")
    })
    public CommonResult<PageResponse<ClassStudent>> getStudent(
            @RequestParam Integer classId,
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize) {
        PageResponse<ClassStudent> classStudent = classService.getClassStudent(pageNo, pageSize, classId);
        return CommonResult.<PageResponse<ClassStudent>>builder()
                .data(classStudent)
                .build();
    }

    @GetMapping("/getUserClass")
    @ApiOperation("获取用户班级")
    public CommonResult<Object> getUserClass(HttpServletRequest request) {
        TokenVo userInfoByToken = JwtUtils.getUserInfoByToken(request);
        Class teacher = classService.getOne(new QueryWrapper<Class>().eq("id", userInfoByToken.getClassId()));
        return CommonResult.<Object>builder().data(teacher).build();
    }
} 